package com.yorick.common.config.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//主题交换机
@Configuration
public class TopicRabbitConfig {
    public final static String first="topic.first";
    public final static String second="topic.second";

    @Bean
    public Queue firstQueue(){
        return new Queue(TopicRabbitConfig.first);
    }

    @Bean
    public Queue secondQueue(){
        return new Queue(TopicRabbitConfig.second);
    }

    @Bean
    TopicExchange topicExchange(){
        return new TopicExchange("TopicExchange");
    }

    @Bean
    Binding bindingFirstTopicExchange(){
        //将firstQueue和topicExchange绑定,而且绑定的键值为topic.first
        //这样只要是消息携带的路由键是topic.first,才会分发到该队列
        return BindingBuilder.bind(firstQueue()).to(topicExchange()).with(TopicRabbitConfig.first);
    }

    @Bean
    Binding bindingSecondTopicExchange(){
        //将firstQueue和topicExchange绑定,而且绑定的键值为topic.#
        //这样只要是消息携带的路由键是topic.开头,才会分发到该队列
        return BindingBuilder.bind(secondQueue()).to(topicExchange()).with("topic.#");
    }

}
